package com.ruoyi.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
import com.ruoyi.exception.BizException;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


//后端通过AppKey和AppSecret获取access_token的工具类
public class DingDingUtil {
    private static Logger logger = LoggerFactory.getLogger(DingDingUtil.class);
    public static final String LANGUAGE = "zh_CN";

    public static String getAccessToken(String appKey, String appSecret) throws BizException {
        DefaultDingTalkClient client = new
                DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            logger.error("dingUtil getAppToken error", e);
            throw new BizException("获取token失败");
        }
        if (!response.isSuccess()) {
            logger.error("dingUtil getAccessTokenAfresh failed, errorCode={}, errorMsg={}", response.getErrcode(), response.getErrmsg());
            return null;
        }
        return response.getAccessToken();
    }

    public static String getDingDingUserInfo(String d_user_id,String access_token) {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(d_user_id);
            req.setLanguage(LANGUAGE);
            OapiV2UserGetResponse rsp = client.execute(req, access_token);
            System.out.println(rsp.getBody());
            return rsp.getBody();
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }



    public static String getDingDingUserId(String code, String accessToken) throws ApiException {
//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
        OapiV2UserGetuserinfoRequest request = new OapiV2UserGetuserinfoRequest();
        request.setCode(code);
        request.setHttpMethod("GET");
        OapiV2UserGetuserinfoResponse response = client.execute(request, accessToken);
        if (!response.isSuccess()) {
            logger.error("dingUtil getUserInfo failed, errorCode={}, errorMsg={}", response.getErrcode(), response.getErrmsg());
            return null;
        }
        return response.getResult().getUserid();
    }
}
